import scopt.test.common
from scopt.mpu.security.db_registry.handlers import flyby_login_password
from scopt.test.common import DEFAULT_LOGIN
from scopt.test.common import DEFAULT_PASSWORD


class FlybyLoginPassword(scopt.test.common.ScoptTest):
    '''Flyby login password tests
    MPU should:

    1. test_valid_token
        send dummy request with valid username and valid password
    2. test_invalid_login
        send dummy request with invalid usename and valid password
    3. test_invalid_password
        send dummy request with valid username and invalid password
    '''
    def test_valid_token(self):
        handler = flyby_login_password.handle
        token = handler.TOKEN()
        token['login'] = DEFAULT_LOGIN
        token['password'] = DEFAULT_PASSWORD
        response = self.send_request(self.dummy_request(), token)
        self.assertEqual(response['resh']['status'], 'OK')

    def test_invalid_login(self):
        handler = flyby_login_password.handle
        token = handler.TOKEN()
        token['login'] = 'invalid login'
        token['password'] = DEFAULT_PASSWORD
        response = self.send_request(self.dummy_request(), token,
                                     pass_interrupts=True)
        self.__assert_wrong_data(response)

    def test_invalid_password(self):
        handler = flyby_login_password.handle
        token = handler.TOKEN()
        token['login'] = DEFAULT_LOGIN
        token['password'] = 'invalid password'
        response = self.send_request(self.dummy_request(), token,
                                     pass_interrupts=True)
        self.__assert_wrong_data(response)

    def __assert_wrong_data(self, response):
        self.assertEqual(response['resh']['status'], 'INT')
        self.assertIsNotNone(response['resd']['text'])
        self.assertEqual(response['resd']['reason'], 'EXCEPTION')
        self.assertEqual(response['resd']['code'],
                         'security.invalid.login.password')
